#@Project HyperNetwork/MatrixHyperNetwork
#@CreatTime 2022/4/22
#@Author 李殊哲
#@E-mail ShuZheLi@126.com
#@Descripes 通过邻接表实现的超图网络

class LinkedHyperNetwork:
    def __init__(self,n):
        self.adjacencyList=[None]*n
        self.NodeNum=n
        self.EageNum=0

    def Connect(self,nodes):
        if self.IsConnect(nodes):
            return False
        for n in nodes:
            edge={'nodes':nodes,'next':self.adjacencyList[n]}
            self.adjacencyList[n]=edge
        self.EageNum += 1
        return True

    def IsConnect(self,nodes):
        x=self.adjacencyList[nodes[0]]
        while x is not None:
            if x['nodes']==nodes:
                return True
        return False

    def Disconnect(self,nodes):
        for n in nodes:
            isConnect=False
            x = self.adjacencyList[n]
            if x['nodes']==nodes:
                self.adjacencyList[n]=self.adjacencyList[n].next
                isConnect=True
            else:
                while x['next'] is not None:
                    if x['next']['nodes'] == nodes:
                        x['next']=x['next']['next']
                        isConnect=True
            if not isConnect:
                return False
        self.EageNum -= 1
        return True

